################################## 
############ Preamble ############ 
################################## 

# set language to English
Sys.setenv(LANG = "en")

# clean up
rm(list = ls())

# Set working directory: please set your own
#setwd("~/Dropbox/JOP third submission/JOP replication/")

# Load necessary packages
library(tidyverse)
library(haven)

# Import dataset with findings
jk_ddd <- read.dta13("01_data/jackknives_ddd.dta")

# Make the plot
jk_ddd_plot <-  jk_ddd %>%
  filter(Region > 0) %>%
  mutate(ci_lower_90 = coef - 1.645 * stderr) %>%
  mutate(ci_upper_90 = coef + 1.645 * stderr) %>%
  ggplot(aes(x = Region, y = coef)) +
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  coord_flip( ) + 
  geom_linerange(aes(ymin = ci_lower, ymax = ci_upper), position = position_dodge(width = 0.75) ) + 
  geom_linerange(aes(ymin = ci_lower, ymax = ci_upper), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower_90, ymax = ci_upper_90), position = position_dodge(width = 0.75), size = 1) +
  geom_point(size = 1, position = position_dodge(width = 0.75) ) + 
  theme_minimal(base_size = 9) +
  scale_x_continuous(limits = c(1, 19)) +
  labs(y = "Coefficient for 2019 EP election in Ciutadella", x = "Code of region removed") +
  theme_bw() +
  facet_wrap(~Model, nrow = 1, ncol = 4) 
jk_ddd_plot

ggsave("04_plots/figured2.png", plot = jk_ddd_plot, width = 25, height = 15, units = "cm")
